'----------------------------------------------
' Name: Ali Karbassi & Chris Hanegraaf
' Date: Date the project is created
' Description: Assignment 5
'              Form frmGrid
'----------------------------------------------
Public Class frmGrid

   ' Private fields
   Private _mainForm As Form
   Private _table As DataTable
   Private _adapter As OleDb.OleDbDataAdapter

   ''' <summary>
   ''' Public properties for Form
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property MainForm() As Form
      Get
         Return _mainForm
      End Get
      Set(ByVal value As Form)
         _mainForm = value
      End Set
   End Property

   ''' <summary>
   ''' Public properties for Data Table
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property Table() As DataTable
      Get
         Return _table
      End Get
      Set(ByVal value As DataTable)
         _table = value
      End Set
   End Property

   ''' <summary>
   ''' Public properties for Adapter
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property Adapter() As OleDb.OleDbDataAdapter
      Get
         Return _adapter
      End Get
      Set(ByVal value As OleDb.OleDbDataAdapter)
         _adapter = value
      End Set
   End Property

   ''' <summary>
   ''' Set datasource and bindingsource
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                    Handles MyBase.Load

      BindingSource.DataSource = _table
      BindingNavigator.BindingSource = BindingSource
      DataGridView.DataSource = BindingSource

   End Sub

   ''' <summary>
   ''' Button Back
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                    Handles btnBack.Click

      Me.Hide()
      _mainForm.Show()

   End Sub

   ''' <summary>
   ''' Updates the database
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks>Not working for some reason</remarks>
   Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                    Handles btnUpdate.Click

      Try
         Try
            Me.BindingContext(_table).EndCurrentEdit()
            _table.GetChanges.AcceptChanges()
            _adapter.Update(_table)
         Catch ex As Exception
         End Try
      Catch ex As Exception
         MsgBox(ex.Message)
      End Try

   End Sub

   ''' <summary>
   ''' Rejects all the changes and reloads the view.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                                    Handles btnReload.Click

      Try
         _table.Clear()
         _adapter.Fill(_table)
         DataGridView.Update()
      Catch ex As Exception
         MsgBox(ex.Message)
      End Try

   End Sub

End Class
